package cn.kgm.user.server.controller;

import cn.kgm.core.http.request.BaseController;
import cn.kgm.core.http.response.R;
import cn.kgm.user.dto.dto.UserTypeSysSaveDTO;
import cn.kgm.user.dto.dto.UserTypeSysUpdateDTO;
import cn.kgm.user.dto.entity.UserTypeSys;
import cn.kgm.user.dto.query.UserTypeSysPageQuery;
import cn.kgm.user.dto.vo.UserTypeSysVO;
import cn.kgm.user.server.service.UserTypeSysService;
import cn.kgm.user.server.struct.UserTypeSysMappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * # 技术改变自己 #
 * toB客户
 * @author kappi
 * @date   2024-04-08 15:08:08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/userTypeSys")
@Api(value = "UserTypeSys", tags = "toB客户")
public class UserTypeSysController extends BaseController {

    @Autowired
    private UserTypeSysService userTypeSysService;

    /**
     * 分页列表查询toB客户
     */
    @ApiOperation(value = "分页查询", notes = "分页查询toB客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "long", paramType = "query", defaultValue = "20"),
    })
    @GetMapping(value = "/page")
    public R<IPage<UserTypeSysVO>> page(UserTypeSysPageQuery data) {
        IPage<UserTypeSys> page = getPage();
        // 构建不为null的查询条件
        UserTypeSys userTypeSys =UserTypeSysMappers.INSTANCE.mapToEntity(data);
        LambdaQueryWrapper<UserTypeSys> wrapper = Wrappers.lambdaQuery(userTypeSys);
        // 分页查询
            userTypeSysService.page(page, wrapper);
        return R.success(page.convert((UserTypeSysMappers.INSTANCE::mapToEntity)));
    }

    /**
     * 根据id查询toB客户详情
     */
    @ApiOperation(value = "查看详情", notes = "根据id查询toB客户详情")
    @GetMapping(value = "/{id}")
    public R<UserTypeSysVO> get(@PathVariable(value = "id") Long id) {
        UserTypeSys userTypeSys =userTypeSysService.getById(id);
        return R.success(UserTypeSysMappers.INSTANCE.mapToEntity(userTypeSys));
    }

    /**
     * 新增toB客户
     */
    @ApiOperation(value = "新增", notes = "新增toB客户")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Validated UserTypeSysSaveDTO data) {
        UserTypeSys userTypeSys =UserTypeSysMappers.INSTANCE.mapToEntity(data);
            userTypeSysService.save(userTypeSys);
        return R.success();
    }

    /**
     * 修改toB客户
     */
    @ApiOperation(value = "修改", notes = "修改toB客户")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody @Validated UserTypeSysUpdateDTO data) {
        UserTypeSys userTypeSys =UserTypeSysMappers.INSTANCE.mapToEntity(data);
        userTypeSysService.updateById(userTypeSys);
        return R.success();
    }

    /**
     * 根据id更新toB客户的删除状态
     */
    @ApiOperation(value = "删除", notes = "根据id更新toB客户的删除状态")
    @GetMapping(value = "/delete/{ids}")
    public R<Boolean> delete(@PathVariable(value = "ids") Long[] ids) {
            userTypeSysService.update(Wrappers.<UserTypeSys>lambdaUpdate()
                .set(UserTypeSys::getIsDel, true)
                .in(UserTypeSys::getId, ids));
        return R.success();
    }
}
